
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-if="checkPermission(['newadd'])" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="年月" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.year }}年{{ scope.row.month }}月</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="正常上班小时数" width="130">
        <template slot-scope="scope">
          <span>{{ scope.row.workingHours }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="加班小时数" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.overtimeHours }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="休假小时数" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.vacationHours }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="天数" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.days }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基数" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.baseHours }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上月20号无事故天数" width="130">
        <template slot-scope="scope">
          <span>{{ scope.row.lastDays }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上月20号无事故时间数" width="130">
        <template slot-scope="scope">
          <span>{{ scope.row.lastHours }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="录入人" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.createUser.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="录入时间" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(['newedit'])" type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <!-- <el-button v-if="checkPermission(['newdel'])" type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageQ" :limit.sync="listQuery.limit" @pagination="fechData" />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="ID" prop="baseId">
          <el-input v-model="temp.baseId" readonly />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="temp.year" />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-input v-model="temp.month" />
        </el-form-item>
        <el-form-item label="正常上班小时数" prop="workingHours">
          <el-input v-model="temp.workingHours" />
        </el-form-item>
        <el-form-item label="加班小时数" prop="overtimeHours">
          <el-input v-model="temp.overtimeHours" />
        </el-form-item>
        <el-form-item label="休假小时数" prop="vacationHours">
          <el-input v-model="temp.vacationHours" />
        </el-form-item>
        <el-form-item label="天数" prop="days">
          <el-input v-model="temp.days" />
        </el-form-item>
        <el-form-item label="上月20号无事故天数" prop="lastDays">
          <el-input v-model="temp.lastDays" />
        </el-form-item>
        <el-form-item label="上月20号无事故时间数" prop="lastHours">
          <el-input v-model="temp.lastHours" />
        </el-form-item>
        <el-form-item label="长期目标" prop="longdate">
          <el-input v-model="temp.longdate" />
        </el-form-item>
        <el-form-item label="短期目标" prop="shortdate">
          <el-input v-model="temp.shortdate" />
        </el-form-item>
        <el-form-item label="6个月到达日" prop="arriveDate">
          <el-input v-model="temp.arriveDate" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginDate">
          <el-input v-model="temp.beginDate" />
        </el-form-item>
        <el-form-item label="录入人">

          <el-autocomplete
            v-model="userName"
            class="el-input"
            :fetch-suggestions="userSearchAsync"
            placeholder="请输入工号"
            :trigger-on-focus="false"
            @select="handleSelect"
          />
        </el-form-item>
        <el-form-item label="录入时间" prop="createDate">
          <el-date-picker v-model="temp.createDate" type="datetime" placeholder="请选择日期" />
          <!-- <el-input v-model="temp.createDate " /> -->
        </el-form-item>
        <el-form-item label="事故发生时间" prop="accidentDate">
          <el-date-picker v-model="temp.accidentDate" type="datetime" placeholder="请选择日期" />
          <!-- <el-input v-model="temp.createDate " /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchBaseHoursList, createBaseHours, editBaseHours, deleteData } from '@/api/baseHours'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import checkPermission from '@/utils/permission'
import { getuserbyemp } from '@/api/user'
export default {
  name: 'BaseHours',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      editList: null,
      sortable: null,
      oldList: [],
      newList: [],
      userName: null,
      userId: null,
      addArr: null,
      addFileName: null,
      resetAdd: null,
      addName: null,
      dialogAddFile: null,
      // 参数条件
      listQuery: {
        pageQ: 1,
        limit: 20
      },
      temp: {
        baseId: undefined,
        year: null,
        month: null,
        days: null,
        workingHours: null,
        overtimeHours: null,
        vacationHours: null,
        lastDays: null,
        lastHours: null,
        accidentDate: null,
        createDate: null,
        longdate: null,
        shortdate: null,
        arriveDate: null,
        beginDate: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '创建',
        detail: '明细'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'sdate', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.fechData()
  },
  methods: {
    fechData() {
      fetchBaseHoursList(this.listQuery).then(response => {
        this.listLoading = true
        this.list = response.ehsbasehoursList
        this.showList = response.ehsbasehoursList
        this.listLoading = false
        this.total = response.count
      })
    },

    checkPermission,
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const query =
          { beginDate: this.temp.beginDate, arriveDate: this.temp.arriveDate, longdate: this.temp.longdate,
            shortdate: this.temp.shortdate, accidentDate: this.temp.accidentDate, userName: this.userName, userId: this.userId, createDate: this.temp.createDate, days: this.temp.days, year: this.temp.year, month: this.temp.month, workingHours: this.temp.workingHours, overtimeHours: this.temp.overtimeHours, vacationHours: this.temp.vacationHours, lastDays: this.temp.lastDays, lastHours: this.temp.lastHours }
          createBaseHours(query).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            // location.reload()
          })
        }
      })
    },

    handleFilter() {
      this.listQuery.pageQ = 1
      this.fechData()
    },
    handleEdit(row) {
      alert(row.baseId)
      const row1 = JSON.parse(JSON.stringify(row))
      this.temp1 = Object.assign({}, row1) // copy obj
      this.temp.baseId = this.temp1.baseId
      this.temp.year = this.temp1.year
      this.temp.month = this.temp1.month
      this.temp.days = this.temp1.days
      this.temp.workingHours = this.temp1.workingHours
      this.temp.overtimeHours = this.temp1.overtimeHours
      this.temp.vacationHours = this.temp1.vacationHours
      this.temp.lastDays = this.temp1.lastDays
      this.temp.lastHours = this.temp1.lastHours
      this.temp.createDate = this.temp1.createDate
      this.temp.accidentDate = this.temp1.accidentDate
      this.temp.longdate = this.temp1.longdate
      this.temp.shortdate = this.temp1.shortdate
      this.temp.arriveDate = this.temp1.arriveDate
      this.temp.beginDate = this.temp1.beginDate
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      if (this.dialogStatus === 'detail') {
        location.reload()
      } else if (this.dialogStatus === 'update') {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            const query1 = JSON.parse(JSON.stringify(tempData))
            tempData.data = +new Date(tempData.data) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
            const query =
            { beginDate: query1.beginDate, arriveDate: query1.arriveDate, longdate: query1.longdate, shortdate: query1.shortdate, baseId: this.temp.baseId, accidentDate: query1.accidentDate, createDate: query1.createDate, days: query1.days, year: query1.year, month: query1.month, workingHours: query1.workingHours, overtimeHours: query1.overtimeHours, vacationHours: query1.vacationHours, lastDays: query1.lastDays, lastHours: query1.lastHours }
            editBaseHours(query).then(response => {

            })
            // 刷新页面
            location.reload()
          }
        })
      }
    },
    handleDetail(row) {
      const row1 = JSON.parse(JSON.stringify(row))
      this.temp = Object.assign({}, row1) // copy obj

      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'detail'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      alert(row.baseId)
      deleteData(row.baseId).then(response => {

      })
      // 刷新页面
      location.reload()
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    async handleSelect(u) {
      this.userName = u.userName
      this.userId = u.userId
      // alert(this.userId)
    },
    async userSearchAsync(queryString, callback) {
      var list = []
      const { userList } = await getuserbyemp(queryString)
      userList.forEach(r => {
        var tem_obj = {}
        tem_obj.value = r.empNo + '-' + r.userName + '-' + r.userId
        tem_obj.userName = r.userName
        tem_obj.userId = r.userId
        list.push(tem_obj)
      })

      callback(list)
    },
    setSort() {
      const el = this.list[0]

      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost',
        setData: function(dataTransfer) {
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)

          const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    }
  }
}
</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>
